/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "It_fs_performance_test.h"

using namespace testing::ext;

std::string g_relativePathExists = "./rel_exists.txt";
std::string g_absolutePathExists = "/userdata/abs_exists.txt";
std::string g_relativePathNotExists = "./rel_not_exists.txt";
std::vector<std::string> g_files;
int scan_flag;

int PreFileTest(void) {
    int fd;
    int ret;
    std::string msg = "this is a test message";

    fd = open(g_relativePathExists.c_str(), O_RDWR | O_CREAT);
    ICUNIT_ASSERT_NOT_EQUAL(fd, -1, fd);
    ret = write(fd, msg.c_str(), msg.length());
    ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
    close(fd);

    fd = open(g_absolutePathExists.c_str(), O_RDWR | O_CREAT);
    ICUNIT_ASSERT_NOT_EQUAL(fd, -1, fd);
    ret = write(fd, msg.c_str(), msg.length());
    ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
    close(fd);

    return 0;
}

void ReadDirectory(std::string path)
{
    struct dirent *ep;
    struct stat st;
	std::string tempPath;
    DIR *dp;
    int ret;

    if (g_files.size() >= 200) {
        return;
    }

    ret = stat(path.c_str(), &st);
    if (ret != 0) {
        return;
    }

    tempPath.assign(path);

    if ((st.st_mode & S_IFMT) == S_IFDIR) {
        dp = opendir(tempPath.c_str());
        while (ep = readdir(dp)) {
            if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..")) {
                continue;
            }
            ReadDirectory(tempPath + "/" + (ep->d_name));
        }
    } else {
        g_files.push_back(tempPath);
        return;
    }
    closedir(dp);
}

namespace OHOS {
class PerformanceFsTest : public testing::Test {
public:
    static void SetUpTestCase(void) {
        int ret = PreFileTest();
        std::string readPath = "/test";
        if (ret == 1) printf("failed to create file\n");
        ReadDirectory(readPath);
    }
    static void TearDownTestCase(void) {
        remove(g_relativePathExists.c_str());
        remove(g_absolutePathExists.c_str());
	}
    void SetUp(void) {

    }
    void TearDown(void) {
        remove(g_relativePathNotExists.c_str());
    }
};

#if defined(LOSCFG_USER_TEST_SMOKE)
HW_PERF_TEST_F(PerformanceFsTest, ItPerformanceFs001, TestSize.Level0, "open relative path when file exists", "1000");

HW_PERF_TEST_F(PerformanceFsTest, ItPerformanceFs002, TestSize.Level0, "open absolute path when file exists", "1000");

HW_PERF_TEST_F(PerformanceFsTest, ItPerformanceFs003, TestSize.Level0, "open absolute path when file not exists", "1000");

HW_PERF_TEST_F(PerformanceFsTest, ItPerformanceFs004, TestSize.Level0, "read file", "1");

HW_PERF_TEST_F(PerformanceFsTest, ItPerformanceFs005, TestSize.Level0, "write file and call sync", "1");

HW_PERF_TEST_F(PerformanceFsTest, ItPerformanceFs006, TestSize.Level0, "write file and call fsync", "1");

HW_PERF_TEST_F(PerformanceFsTest, ItPerformanceFs007, TestSize.Level0, "read big file", "1");

HW_PERF_TEST_F(PerformanceFsTest, ItPerformanceFs008, TestSize.Level0, "lseek test", "1");

HW_PERF_TEST_F(PerformanceFsTest, ItPerformanceFs009, TestSize.Level0, "open amount of files", "1");
#endif
}